public class BinarySearchTree {

    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    /**
     * 查找节点:
     *   最好情况下：满二叉树/完全二叉树 O(logN)
     *   最坏情况下：单分支的树 O(N)
     */
    public TreeNode search(int key) {
        if(root == null){
            return null;
        }
        TreeNode cur = root;
        while(cur != null){
            if(cur.val < key){
                cur = cur.right;
            }else if(cur.val > key){
                cur = cur.left;
            }else {
                return cur;
            }
        }
        return null;
    }

    // 插入一个元素
    public boolean insert(int key) {
        if(root == null){
            root = new TreeNode(key);
            return true;
        }
        TreeNode node = new TreeNode(key);
        TreeNode parent = null; // 记录前一个，"存储路径"
        TreeNode cur = root;
        while(cur != null){
            if(cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if(cur.val > key){
                parent = cur;
                cur = cur.left;
            }else {
                return false;
            }
        }
        if(parent.val < key){
            parent.right = node;
        }else {
            parent.left = node;
        }
        return true;
    }

    //删除key的值
    public void remove(int key) {
        if(root == null){
            return;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while(cur != null){
            if(cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if(cur.val == key){
                removeNode(parent,cur);
                return;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null){
            if(cur == root){
                root = cur.right;
            }else if(parent.left == cur){ // cur != root
                parent.left = cur.right;
            }else {
                // parent.right == cur
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = cur.left;
            }else if(parent.left == cur){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            // cur.left != null && cur.right != null
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while(target.left != null){
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.left == target){
                targetParent.left = target.right;
            }else {
                // targetParent.right == target
                targetParent.right = target.right;
            }
        }
    }

}